<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.3.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">
  <meta name="baidu-site-verification" content="code-OyDaGkZ9eq">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"example.com","root":"/","scheme":"Gemini","version":"7.8.0","exturl":true,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":"mac"},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":true,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="本节课程目标 了解shell中的通配符 熟悉grep、cut、sort等小工具和shell中的通配符的使用  #一、文本处理工具">
<meta property="og:type" content="article">
<meta property="og:title" content="01_SHELL编程前奏_小工具">
<meta property="og:url" content="http://example.com/2021/01/01/%E5%B0%8F%E7%94%9F%E5%8D%9A%E5%AE%A2/linux/01_SHELL%E7%BC%96%E7%A8%8B%E5%89%8D%E5%A5%8F_%E5%B0%8F%E5%B7%A5%E5%85%B7/index.html">
<meta property="og:site_name" content="极风键客">
<meta property="og:description" content="本节课程目标 了解shell中的通配符 熟悉grep、cut、sort等小工具和shell中的通配符的使用  #一、文本处理工具">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2021-01-01T12:46:25.000Z">
<meta property="article:modified_time" content="2021-02-18T00:20:42.913Z">
<meta property="article:author" content="王增明">
<meta property="article:tag" content="linux">
<meta property="article:tag" content="shell">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="http://example.com/2021/01/01/%E5%B0%8F%E7%94%9F%E5%8D%9A%E5%AE%A2/linux/01_SHELL%E7%BC%96%E7%A8%8B%E5%89%8D%E5%A5%8F_%E5%B0%8F%E5%B7%A5%E5%85%B7/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'en'
  };
</script>

  <title>01_SHELL编程前奏_小工具 | 极风键客</title>
  


  <script data-pjax>
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?b94f963e94e4127fa71ab49cbddce93b";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>




  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

<link rel="alternate" href="/atom.xml" title="极风键客" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">极风键客</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">技术狂热爱好者!</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
        <li class="menu-item menu-item-sitemap">

    <a href="/sitemap.xml" rel="section"><i class="fa fa-sitemap fa-fw"></i>站点地图</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>
  <a role="button" class="book-mark-link book-mark-link-fixed"></a>

  <span class="exturl github-corner" data-url="aHR0cHM6Ly9naXRodWIuY29tL3dhbmd6ZW5nbWluZw==" title="客官,来嘛~" aria-label="客官,来嘛~"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></span>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="en">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/01/01/%E5%B0%8F%E7%94%9F%E5%8D%9A%E5%AE%A2/linux/01_SHELL%E7%BC%96%E7%A8%8B%E5%89%8D%E5%A5%8F_%E5%B0%8F%E5%B7%A5%E5%85%B7/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpeg">
      <meta itemprop="name" content="王增明">
      <meta itemprop="description" content="喜欢折腾的极客聚集地.">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="极风键客">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          01_SHELL编程前奏_小工具
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2021-01-01 20:46:25" itemprop="dateCreated datePublished" datetime="2021-01-01T20:46:25+08:00">2021-01-01</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2021-02-18 08:20:42" itemprop="dateModified" datetime="2021-02-18T08:20:42+08:00">2021-02-18</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/linux/" itemprop="url" rel="index"><span itemprop="name">linux</span></a>
                </span>
            </span>

          
            <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv" style="display: none;">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span id="busuanzi_value_page_pv"></span>
            </span><br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="far fa-file-word"></i>
              </span>
                <span class="post-meta-item-text">本文字数：</span>
              <span>10k</span>
            </span>
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="far fa-clock"></i>
              </span>
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              <span>9 分钟</span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h1 id="本节课程目标"><a href="#本节课程目标" class="headerlink" title="本节课程目标"></a>本节课程目标</h1><ul>
<li>了解shell中的通配符</li>
<li>熟悉grep、cut、sort等小工具和shell中的通配符的使用</li>
</ul>
<p>#一、文本处理工具</p>
<a id="more"></a>

<h2 id="1-grep工具"><a href="#1-grep工具" class="headerlink" title="1. grep工具"></a>1. grep工具</h2><blockquote>
<p>grep是**==行==**过滤工具；用于根据关键字进行行过滤</p>
</blockquote>
<h3 id="语法和选项"><a href="#语法和选项" class="headerlink" title="语法和选项"></a>语法和选项</h3><p><strong>语法：</strong></p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># grep [选项] &#x27;关键字&#x27; 文件名</span></span><br></pre></td></tr></table></figure>
<p><strong>常见选项：</strong></p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">OPTIONS:</span><br><span class="line">    <span class="literal">-i</span>: 不区分大小写</span><br><span class="line">    <span class="literal">-v</span>: 查找不包含指定内容的行,反向选择</span><br><span class="line">    <span class="literal">-w</span>: 按单词搜索</span><br><span class="line">    <span class="literal">-o</span>: 打印匹配关键字</span><br><span class="line">    <span class="literal">-c</span>: 统计匹配到的行数</span><br><span class="line">    <span class="literal">-n</span>: 显示行号</span><br><span class="line">    <span class="literal">-r</span>: 逐层遍历目录查找</span><br><span class="line">    <span class="literal">-A</span>: 显示匹配行及后面多少行	</span><br><span class="line">    <span class="literal">-B</span>: 显示匹配行及前面多少行</span><br><span class="line">    <span class="literal">-C</span>: 显示匹配行前后多少行</span><br><span class="line">    <span class="literal">-l</span>：只列出匹配的文件名</span><br><span class="line">    <span class="literal">-L</span>：列出不匹配的文件名</span><br><span class="line">    <span class="literal">-e</span>: 使用正则匹配</span><br><span class="line">    <span class="literal">-E</span>:使用扩展正则匹配</span><br><span class="line">    ^key:以关键字开头</span><br><span class="line">    key<span class="variable">$</span>:以关键字结尾</span><br><span class="line">    ^<span class="variable">$</span>:匹配空行</span><br><span class="line">    -<span class="literal">-color</span>=auto ：可以将找到的关键词部分加上颜色的显示</span><br></pre></td></tr></table></figure>
<p><strong>颜色显示（别名设置）：</strong></p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">临时设置：</span><br><span class="line"><span class="comment"># alias grep=&#x27;grep --color=auto&#x27;			//只针对当前终端和当前用户生效</span></span><br><span class="line"></span><br><span class="line">永久设置：</span><br><span class="line"><span class="number">1</span>）全局（针对所有用户生效）</span><br><span class="line">vim /etc/bashrc</span><br><span class="line">alias grep=<span class="string">&#x27;grep --color=auto&#x27;</span></span><br><span class="line">source /etc/bashrc</span><br><span class="line"></span><br><span class="line"><span class="number">2</span>）局部（针对具体的某个用户）</span><br><span class="line">vim ~/.bashrc</span><br><span class="line">alias grep=<span class="string">&#x27;grep --color=auto&#x27;</span></span><br><span class="line">source ~/.bashrc</span><br></pre></td></tr></table></figure>
<p><strong>举例说明：</strong></p>
<p>==说明：不要直接使用/etc/passwd文件，将其拷贝到/tmp下做实验！==</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># grep -i root passwd						忽略大小写匹配包含root的行</span></span><br><span class="line"><span class="comment"># grep -w ftp passwd 						精确匹配ftp单词</span></span><br><span class="line"><span class="comment"># grep -w hello passwd 						精确匹配hello单词;自己添加包含hello的行到文件</span></span><br><span class="line"><span class="comment"># grep -wo ftp passwd 						打印匹配到的关键字ftp</span></span><br><span class="line"><span class="comment"># grep -n root passwd 						打印匹配到root关键字的行好</span></span><br><span class="line"><span class="comment"># grep -ni root passwd 						忽略大小写匹配统计包含关键字root的行</span></span><br><span class="line"><span class="comment"># grep -nic root passwd						忽略大小写匹配统计包含关键字root的行数</span></span><br><span class="line"><span class="comment"># grep -i ^root passwd 						忽略大小写匹配以root开头的行</span></span><br><span class="line"><span class="comment"># grep bash$ passwd 							匹配以bash结尾的行</span></span><br><span class="line"><span class="comment"># grep -n ^$ passwd 							匹配空行并打印行号</span></span><br><span class="line"><span class="comment"># grep ^# /etc/vsftpd/vsftpd.conf		匹配以#号开头的行</span></span><br><span class="line"><span class="comment"># grep -v ^# /etc/vsftpd/vsftpd.conf	匹配不以#号开头的行</span></span><br><span class="line"><span class="comment"># grep -A 5 mail passwd 				 	匹配包含mail关键字及其后5行</span></span><br><span class="line"><span class="comment"># grep -B 5 mail passwd 				 	匹配包含mail关键字及其前5行</span></span><br><span class="line"><span class="comment"># grep -C 5 mail passwd 					匹配包含mail关键字及其前后5行</span></span><br></pre></td></tr></table></figure>
<h2 id="2-cut工具"><a href="#2-cut工具" class="headerlink" title="2. cut工具"></a>2. cut工具</h2><blockquote>
<p>cut是**==列==**截取工具，用于列的截取</p>
</blockquote>
<h3 id="语法和选项-1"><a href="#语法和选项-1" class="headerlink" title="语法和选项"></a>语法和选项</h3><p><strong>语法：</strong></p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># cut 选项  文件名</span></span><br></pre></td></tr></table></figure>
<p><strong>常见选项：</strong></p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="literal">-c</span>:	以字符为单位进行分割,截取</span><br><span class="line"><span class="literal">-d</span>:	自定义分隔符，默认为制表符\t</span><br><span class="line"><span class="operator">-f</span>:	与<span class="literal">-d</span>一起使用，指定截取哪个区域</span><br></pre></td></tr></table></figure>
<p><strong>举例说明:</strong></p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># cut -d: -f1 1.txt 			以:冒号分割，截取第1列内容</span></span><br><span class="line"><span class="comment"># cut -d: -f1,6,7 1.txt 	以:冒号分割，截取第1,6,7列内容</span></span><br><span class="line"><span class="comment"># cut -c4 1.txt 				截取文件中每行第4个字符</span></span><br><span class="line"><span class="comment"># cut -c1-4 1.txt 			截取文件中每行的1-4个字符</span></span><br><span class="line"><span class="comment"># cut -c4-10 1.txt 			截取文件中每行的4-10个字符</span></span><br><span class="line"><span class="comment"># cut -c5- 1.txt 				从第5个字符开始截取后面所有字符</span></span><br></pre></td></tr></table></figure>
<p><strong>课堂练习：</strong><br>用小工具列出你当系统的运行级别。5/3</p>
<ol>
<li>如何查看系统运行级别<ul>
<li>命令<code>runlevel</code></li>
<li>文件<code>/etc/inittab</code></li>
</ul>
</li>
<li>如何过滤运行级别</li>
</ol>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">runlevel |cut <span class="literal">-c3</span></span><br><span class="line">runlevel | cut <span class="literal">-d</span> <span class="string">&#x27; &#x27;</span> <span class="literal">-f2</span></span><br><span class="line">grep <span class="literal">-v</span> <span class="string">&#x27;^#&#x27;</span> /etc/inittab | cut <span class="literal">-d</span>: <span class="literal">-f2</span></span><br><span class="line">grep <span class="string">&#x27;^id&#x27;</span> /etc/inittab |cut <span class="literal">-d</span>: <span class="literal">-f2</span></span><br><span class="line">grep <span class="string">&quot;initdefault:<span class="variable">$</span>&quot;</span> /etc/inittab | cut <span class="literal">-c4</span></span><br><span class="line">grep <span class="literal">-v</span> ^<span class="comment"># /etc/inittab |cut -c4</span></span><br><span class="line">grep <span class="string">&#x27;id:&#x27;</span> /etc/inittab |cut <span class="literal">-d</span>: <span class="literal">-f2</span></span><br><span class="line">cut <span class="literal">-d</span><span class="string">&#x27;:&#x27;</span> <span class="literal">-f2</span> /etc/inittab |grep <span class="literal">-v</span> ^<span class="comment">#</span></span><br><span class="line">cut <span class="literal">-c4</span> /etc/inittab |tail <span class="literal">-1</span></span><br><span class="line">cut <span class="literal">-d</span>: <span class="literal">-f2</span> /etc/inittab |tail <span class="literal">-1</span></span><br></pre></td></tr></table></figure>
<h2 id="3-sed"><a href="#3-sed" class="headerlink" title="3. sed"></a>3. sed</h2><blockquote>
<p> sed是一种流编辑器，它一次处理一行内容。处理时，把当前处理的行存储在临时缓冲区中，称为“模式空间”，接着用sed命令处理缓冲区中的内容，处理完成后，把缓冲区的内容送往屏幕。接着处理下一行，这样不断重复，直到文件末尾。文件内容并没有改变，除非你使用重定向存储输出。</p>
</blockquote>
<h3 id="语法和选项-2"><a href="#语法和选项-2" class="headerlink" title="语法和选项"></a>语法和选项</h3><p><strong>语法</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sed [选项参数]  ‘command’  filename</span><br></pre></td></tr></table></figure>
<p><strong>选项</strong></p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="literal">-e</span> ：直接在指令列模式上进行sed的动作编辑。</span><br><span class="line"><span class="literal">-a</span> ：新增，a的后面可以接字串，在下一行出现</span><br><span class="line"><span class="literal">-d</span> : 删除</span><br><span class="line"><span class="literal">-s</span> ：查找并替换</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p><strong>举例说明</strong></p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># sed &#x27;2a mei nv&#x27; sed.txt  			将“mei nv”这个单词插入到sed.txt第二行下，打印。</span></span><br><span class="line"><span class="comment"># sed &#x27;/wo/d&#x27; sed.txt 		     	删除sed.txt文件所有包含wo的行</span></span><br><span class="line"><span class="comment"># sed &#x27;s/wo/ni/g&#x27; sed.txt 			将sed.txt文件中wo替换为ni,‘g’表示global，全部替换</span></span><br><span class="line"><span class="comment"># sed -e &#x27;2d&#x27; -e &#x27;s/wo/ni/g&#x27; sed.txt  将sed.txt文件中的第二行删除并将wo替换为ni</span></span><br></pre></td></tr></table></figure>
<h2 id="4-awk"><a href="#4-awk" class="headerlink" title="4. awk"></a>4. awk</h2><blockquote>
<p>一个强大的文本分析工具，把文件逐行的读入，以空格为默认分隔符将每行切片，切开的部分再进行分析处理。</p>
</blockquote>
<h3 id="语法和选项-3"><a href="#语法和选项-3" class="headerlink" title="语法和选项"></a>语法和选项</h3><p><strong>语法</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">awk [选项参数] ‘pattern1&#123;action1&#125;  pattern2&#123;action2&#125;...’ filename	</span><br></pre></td></tr></table></figure>
<ul>
<li>pattern：表示AWK在数据中查找的内容，就是匹配模式</li>
<li>action：在找到匹配内容时所执行的一系列命令    </li>
</ul>
<p><strong>选项</strong></p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="operator">-F</span> ：指定输入文件折分隔符</span><br><span class="line"><span class="literal">-v</span> ：赋值一个用户定义变量</span><br></pre></td></tr></table></figure>
<p><strong>举例说明</strong></p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">搜索passwd文件以root关键字开头的所有行，并输出该行的第<span class="number">7</span>列。</span><br><span class="line"><span class="comment"># awk -F: &#x27;/^root/&#123;print $7&#125;&#x27; passwd </span></span><br><span class="line"></span><br><span class="line">搜索passwd文件以root关键字开头的所有行，并输出该行的第<span class="number">1</span>列和第<span class="number">7</span>列，中间以“，”号分割。</span><br><span class="line"><span class="comment"># awk -F: &#x27;/^root/&#123;print $1&quot;,&quot;$7&#125;&#x27; passwd </span></span><br><span class="line"></span><br><span class="line">只显示/etc/passwd的第一列和第七列，以逗号分割，且在所有行前面添加列名user，shell在最后一行添加<span class="string">&quot;dahaige，/bin/zuishuai&quot;</span>。  </span><br><span class="line"><span class="comment"># awk -F : &#x27;BEGIN&#123;print &quot;user, shell&quot;&#125; &#123;print $1&quot;,&quot;$7&#125; END&#123;print &quot;dahaige,/bin/zuishuai&quot;&#125;&#x27; passwd</span></span><br><span class="line">注意：<span class="keyword">BEGIN</span> 在所有数据读取行之前执行；<span class="keyword">END</span> 在所有数据执行之后执行。</span><br><span class="line"></span><br><span class="line">将passwd文件中的用户id增加数值<span class="number">1</span>并输出</span><br><span class="line"><span class="comment"># awk -v i=1 -F: &#x27;&#123;print $3+i&#125;&#x27; passwd</span></span><br></pre></td></tr></table></figure>
<h2 id="5-sort工具"><a href="#5-sort工具" class="headerlink" title="5. sort工具"></a>5. sort工具</h2><blockquote>
<p>sort工具用于排序;它将文件的每一行作为一个单位，从首字符向后，依次按ASCII码值进行比较，最后将他们按升序输出。</p>
</blockquote>
<h3 id="语法和选项-4"><a href="#语法和选项-4" class="headerlink" title="语法和选项"></a>语法和选项</h3><figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="literal">-u</span> ：去除重复行</span><br><span class="line"><span class="literal">-r</span> ：降序排列，默认是升序</span><br><span class="line"><span class="literal">-o</span> : 将排序结果输出到文件中,类似重定向符号&gt;</span><br><span class="line"><span class="literal">-n</span> ：以数字排序，默认是按字符排序</span><br><span class="line"><span class="literal">-t</span> ：分隔符</span><br><span class="line"><span class="literal">-k</span> ：第N列</span><br><span class="line"><span class="literal">-b</span> ：忽略前导空格。</span><br><span class="line"><span class="literal">-R</span> ：随机排序，每次运行的结果均不同</span><br></pre></td></tr></table></figure>
<p><strong>举例说明</strong></p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># sort -n -t: -k3 1.txt 			按照用户的uid进行升序排列</span></span><br><span class="line"><span class="comment"># sort -nr -t: -k3 1.txt 			按照用户的uid进行降序排列</span></span><br><span class="line"><span class="comment"># sort -n 2.txt 						按照数字排序</span></span><br><span class="line"><span class="comment"># sort -nu 2.txt 						按照数字排序并且去重</span></span><br><span class="line"><span class="comment"># sort -nr 2.txt </span></span><br><span class="line"><span class="comment"># sort -nru 2.txt </span></span><br><span class="line"><span class="comment"># sort -nru 2.txt </span></span><br><span class="line"><span class="comment"># sort -n 2.txt -o 3.txt 			按照数字排序并将结果重定向到文件</span></span><br><span class="line"><span class="comment"># sort -R 2.txt </span></span><br><span class="line"><span class="comment"># sort -u 2.txt </span></span><br></pre></td></tr></table></figure>
<h2 id="6-uniq工具"><a href="#6-uniq工具" class="headerlink" title="6. uniq工具"></a>6. uniq工具</h2><blockquote>
<p>uniq用于去除**==连续==<strong>的</strong>==重复==**行</p>
</blockquote>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">常见选项：</span><br><span class="line"><span class="literal">-i</span>: 忽略大小写</span><br><span class="line"><span class="literal">-c</span>: 统计重复行次数</span><br><span class="line"><span class="literal">-d</span>:只显示重复行</span><br><span class="line"></span><br><span class="line">举例说明：</span><br><span class="line"><span class="comment"># uniq 2.txt </span></span><br><span class="line"><span class="comment"># uniq -d 2.txt </span></span><br><span class="line"><span class="comment"># uniq -dc 2.txt </span></span><br></pre></td></tr></table></figure>
<h2 id="7-tee工具"><a href="#7-tee工具" class="headerlink" title="7. tee工具"></a>7. tee工具</h2><blockquote>
<p>tee工具是从标准输入读取并写入到标准输出和文件，即：双向覆盖重定向（屏幕输出|文本输入）</p>
</blockquote>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">选项：</span><br><span class="line"><span class="literal">-a</span> 双向追加重定向</span><br><span class="line"></span><br><span class="line"><span class="comment"># echo hello world</span></span><br><span class="line"><span class="comment"># echo hello world|tee file1</span></span><br><span class="line"><span class="comment"># cat file1 </span></span><br><span class="line"><span class="comment"># echo 999|tee -a file1</span></span><br><span class="line"><span class="comment"># cat file1 </span></span><br></pre></td></tr></table></figure>
<h2 id="8-diff工具"><a href="#8-diff工具" class="headerlink" title="8. diff工具"></a>8. diff工具</h2><blockquote>
<p>diff工具用于逐行比较文件的不同</p>
</blockquote>
<p>注意：diff描述两个文件不同的方式是告诉我们==怎样改变第一个==文件之后==与第二个文件匹配==。</p>
<h3 id="语法和选项-5"><a href="#语法和选项-5" class="headerlink" title="语法和选项"></a>语法和选项</h3><p><strong>语法：</strong></p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">diff</span> [选项] 文件<span class="number">1</span> 文件<span class="number">2</span></span><br></pre></td></tr></table></figure>
<p><strong>常用选项：</strong></p>
<table>
<thead>
<tr>
<th>选项</th>
<th>含义</th>
<th>备注</th>
</tr>
</thead>
<tbody><tr>
<td>-b</td>
<td>不检查空格</td>
<td></td>
</tr>
<tr>
<td>-B</td>
<td>不检查空白行</td>
<td></td>
</tr>
<tr>
<td>-i</td>
<td>不检查大小写</td>
<td></td>
</tr>
<tr>
<td>-w</td>
<td>忽略所有的空格</td>
<td></td>
</tr>
<tr>
<td>–normal</td>
<td>正常格式显示(默认)</td>
<td></td>
</tr>
<tr>
<td>-c</td>
<td>上下文格式显示</td>
<td></td>
</tr>
<tr>
<td>-u</td>
<td>合并格式显示</td>
<td></td>
</tr>
</tbody></table>
<p><strong>举例说明：</strong></p>
<ul>
<li>比较两个==普通文件==异同，文件准备：</li>
</ul>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">[<span class="type">root</span>@<span class="type">MissHou</span> ~]<span class="comment"># cat file1</span></span><br><span class="line">aaaa</span><br><span class="line"><span class="number">111</span></span><br><span class="line">hello world</span><br><span class="line"><span class="number">222</span></span><br><span class="line"><span class="number">333</span></span><br><span class="line">bbb</span><br><span class="line">[<span class="type">root</span>@<span class="type">MissHou</span> ~]<span class="comment">#</span></span><br><span class="line">[<span class="type">root</span>@<span class="type">MissHou</span> ~]<span class="comment"># cat file2</span></span><br><span class="line">aaa</span><br><span class="line">hello</span><br><span class="line"><span class="number">111</span></span><br><span class="line"><span class="number">222</span></span><br><span class="line">bbb</span><br><span class="line"><span class="number">333</span></span><br><span class="line">world</span><br></pre></td></tr></table></figure>
<p>1）正常显示</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">diff</span>目的：file1如何改变才能和file2匹配</span><br><span class="line">[<span class="type">root</span>@<span class="type">MissHou</span> ~]<span class="comment"># diff file1 file2</span></span><br><span class="line"><span class="number">1</span>c1,<span class="number">2</span>					第一个文件的第<span class="number">1</span>行需要改变(c=change)才能和第二个文件的第<span class="number">1</span>到<span class="number">2</span>行匹配			</span><br><span class="line">&lt; aaaa				小于号<span class="string">&quot;&lt;&quot;</span>表示左边文件(file1)文件内容</span><br><span class="line">---					---表示分隔符</span><br><span class="line">&gt; aaa					大于号<span class="string">&quot;&gt;&quot;</span>表示右边文件(file2)文件内容</span><br><span class="line">&gt; hello</span><br><span class="line"><span class="number">3</span>d3					第一个文件的第<span class="number">3</span>行删除(d=delete)后才能和第二个文件的第<span class="number">3</span>行匹配</span><br><span class="line">&lt; hello world</span><br><span class="line"><span class="number">5</span>d4					第一个文件的第<span class="number">5</span>行删除后才能和第二个文件的第<span class="number">4</span>行匹配</span><br><span class="line">&lt; <span class="number">333</span></span><br><span class="line"><span class="number">6</span>a6,<span class="number">7</span>					第一个文件的第<span class="number">6</span>行增加(a=add)内容后才能和第二个文件的第<span class="number">6</span>到<span class="number">7</span>行匹配</span><br><span class="line">&gt; <span class="number">333</span>					需要增加的内容在第二个文件里是<span class="number">333</span>和world</span><br><span class="line">&gt; world</span><br></pre></td></tr></table></figure>
<p>2）上下文格式显示</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">[<span class="type">root</span>@<span class="type">MissHou</span> ~]<span class="comment"># diff -c file1 file2</span></span><br><span class="line">前两行主要列出需要比较的文件名和文件的时间戳；文件名前面的符号***表示file1，---表示file2</span><br><span class="line">*** file1       <span class="number">2019</span><span class="literal">-04</span><span class="literal">-16</span> <span class="number">16</span>:<span class="number">26</span>:<span class="number">05.748650262</span> +<span class="number">0800</span></span><br><span class="line">--- file2       <span class="number">2019</span><span class="literal">-04</span><span class="literal">-16</span> <span class="number">16</span>:<span class="number">26</span>:<span class="number">30.470646030</span> +<span class="number">0800</span></span><br><span class="line">***************	我是分隔符</span><br><span class="line">*** <span class="number">1</span>,<span class="number">6</span> ****		以***开头表示file1文件，<span class="number">1</span>,<span class="number">6</span>表示<span class="number">1</span>到<span class="number">6</span>行</span><br><span class="line">! aaaa				!表示该行需要修改才与第二个文件匹配</span><br><span class="line">  <span class="number">111</span></span><br><span class="line">- hello world		-表示需要删除该行才与第二个文件匹配</span><br><span class="line">  <span class="number">222</span></span><br><span class="line">- <span class="number">333</span>					-表示需要删除该行才与第二个文件匹配</span><br><span class="line">  bbb</span><br><span class="line">--- <span class="number">1</span>,<span class="number">7</span> ----		以---开头表示file2文件，<span class="number">1</span>,<span class="number">7</span>表示<span class="number">1</span>到<span class="number">7</span>行</span><br><span class="line">! aaa					表示第一个文件需要修改才与第二个文件匹配</span><br><span class="line">! hello				表示第一个文件需要修改才与第二个文件匹配</span><br><span class="line">  <span class="number">111</span></span><br><span class="line">  <span class="number">222</span></span><br><span class="line">  bbb</span><br><span class="line">+ <span class="number">333</span>					表示第一个文件需要加上该行才与第二个文件匹配</span><br><span class="line">+ world				表示第一个文件需要加上该行才与第二个文件匹配</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p>3）合并格式显示</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">[<span class="type">root</span>@<span class="type">MissHou</span> ~]<span class="comment"># diff -u file1 file2</span></span><br><span class="line">前两行主要列出需要比较的文件名和文件的时间戳；文件名前面的符号---表示file1，+++表示file2</span><br><span class="line">--- file1       <span class="number">2019</span><span class="literal">-04</span><span class="literal">-16</span> <span class="number">16</span>:<span class="number">26</span>:<span class="number">05.748650262</span> +<span class="number">0800</span></span><br><span class="line">+++ file2       <span class="number">2019</span><span class="literal">-04</span><span class="literal">-16</span> <span class="number">16</span>:<span class="number">26</span>:<span class="number">30.470646030</span> +<span class="number">0800</span></span><br><span class="line"><span class="selector-tag">@</span><span class="selector-tag">@</span> <span class="literal">-1</span>,<span class="number">6</span> +<span class="number">1</span>,<span class="number">7</span> <span class="selector-tag">@</span><span class="selector-tag">@</span></span><br><span class="line"><span class="literal">-aaaa</span></span><br><span class="line">+aaa</span><br><span class="line">+hello</span><br><span class="line"> <span class="number">111</span></span><br><span class="line"><span class="literal">-hello</span> world</span><br><span class="line"> <span class="number">222</span></span><br><span class="line"><span class="literal">-333</span></span><br><span class="line"> bbb</span><br><span class="line">+<span class="number">333</span></span><br><span class="line">+world</span><br></pre></td></tr></table></figure>
<ul>
<li>比较两个==目录不同==</li>
</ul>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">默认情况下也会比较两个目录里相同文件的内容</span><br><span class="line">[<span class="type">root</span>@<span class="type">MissHou</span>  <span class="type">tmp</span>]<span class="comment"># diff dir1 dir2</span></span><br><span class="line"><span class="built_in">diff</span> dir1/file1 dir2/file1</span><br><span class="line"><span class="number">0</span>a1</span><br><span class="line">&gt; hello</span><br><span class="line">Only <span class="keyword">in</span> dir1: file3</span><br><span class="line">Only <span class="keyword">in</span> dir2: test1</span><br><span class="line">如果只需要比较两个目录里文件的不同，不需要进一步比较文件内容，需要加<span class="literal">-q</span>选项</span><br><span class="line">[<span class="type">root</span>@<span class="type">MissHou</span>  <span class="type">tmp</span>]<span class="comment"># diff -q dir1 dir2</span></span><br><span class="line">Files dir1/file1 and dir2/file1 differ</span><br><span class="line">Only <span class="keyword">in</span> dir1: file3</span><br><span class="line">Only <span class="keyword">in</span> dir2: test1</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p><strong>其他小技巧：</strong></p>
<p>有时候我们需要以一个文件为标准，去修改其他文件，并且修改的地方较多时，我们可以通过打补丁的方式完成。</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1</span>）先找出文件不同，然后输出到一个文件</span><br><span class="line">[<span class="type">root</span>@<span class="type">MissHou</span> ~]<span class="comment"># diff -uN file1 file2 &gt; file.patch</span></span><br><span class="line"><span class="literal">-u</span>:上下文模式</span><br><span class="line"><span class="literal">-N</span>:将不存在的文件当作空文件</span><br><span class="line"><span class="number">2</span>）将不同内容打补丁到文件</span><br><span class="line">[<span class="type">root</span>@<span class="type">MissHou</span> ~]<span class="comment"># patch file1 file.patch</span></span><br><span class="line">patching file file1</span><br><span class="line"><span class="number">3</span>）测试验证</span><br><span class="line">[<span class="type">root</span>@<span class="type">MissHou</span> ~]<span class="comment"># diff file1 file2</span></span><br><span class="line">[<span class="type">root</span>@<span class="type">MissHou</span> ~]<span class="comment">#</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h2 id="9-paste工具"><a href="#9-paste工具" class="headerlink" title="9. paste工具"></a>9. paste工具</h2><blockquote>
<p>paste工具用于合并文件行</p>
</blockquote>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">常用选项：</span><br><span class="line"><span class="literal">-d</span>：自定义间隔符，默认是tab</span><br><span class="line"><span class="literal">-s</span>：串行处理，非并行</span><br></pre></td></tr></table></figure>
<h2 id="10-tr工具"><a href="#10-tr工具" class="headerlink" title="10. tr工具"></a>10. tr工具</h2><blockquote>
<p> tr用于字符转换，替换和删除；主要用于 删除文件中控制字符 或进行 字符转换</p>
</blockquote>
<h3 id="语法和选项-6"><a href="#语法和选项-6" class="headerlink" title="语法和选项"></a>语法和选项</h3><p><strong>语法：</strong></p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">用法<span class="number">1</span>：命令的执行结果交给tr处理，其中string1用于查询，string2用于转换处理</span><br><span class="line"><span class="comment"># commands|tr  &#x27;string1&#x27;  &#x27;string2&#x27;</span></span><br><span class="line">用法<span class="number">2</span>：tr处理的内容来自文件，记住要使用<span class="string">&quot;&lt;&quot;</span>标准输入</span><br><span class="line"><span class="comment"># tr  &#x27;string1&#x27;  &#x27;string2&#x27; &lt; filename</span></span><br><span class="line"></span><br><span class="line">用法<span class="number">3</span>：匹配string1进行相应操作，如删除操作</span><br><span class="line"><span class="comment"># tr options &#x27;string1&#x27; &lt; filename</span></span><br></pre></td></tr></table></figure>
<p><strong>常用选项：</strong></p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="literal">-d</span> 删除字符串<span class="number">1</span>中所有输入字符。</span><br><span class="line"><span class="literal">-s</span> 删除所有重复出现字符序列，只保留第一个；即将重复出现字符串压缩为一个字符串</span><br></pre></td></tr></table></figure>
<p><strong>常匹配字符串：</strong></p>
<table>
<thead>
<tr>
<th>字符串</th>
<th>含义</th>
<th>备注</th>
</tr>
</thead>
<tbody><tr>
<td>==a-z==或[:lower:]</td>
<td>匹配所有小写字母</td>
<td>[a-zA-Z0-9]</td>
</tr>
<tr>
<td>==A-Z==或[:upper:]</td>
<td>匹配所有大写字母</td>
<td></td>
</tr>
<tr>
<td>==0-9==或[:digit:]</td>
<td>匹配所有数字</td>
<td></td>
</tr>
<tr>
<td>[:alnum:]</td>
<td>匹配所有字母和数字</td>
<td></td>
</tr>
<tr>
<td>[:alpha:]</td>
<td>匹配所有字母</td>
<td></td>
</tr>
<tr>
<td>[:blank:]</td>
<td>所有水平空白</td>
<td></td>
</tr>
<tr>
<td>[:punct:]</td>
<td>匹配所有标点符号</td>
<td></td>
</tr>
<tr>
<td>[:space:]</td>
<td>所有水平或垂直的空格</td>
<td></td>
</tr>
<tr>
<td>[:cntrl:]</td>
<td>所有控制字符</td>
<td>\f Ctrl-L        走行换页<br>\n Ctrl-J      换行</td>
</tr>
<tr>
<td>\r Ctrl-M        回车</td>
<td></td>
<td></td>
</tr>
<tr>
<td>\t Ctrl-I      tab键</td>
<td></td>
<td></td>
</tr>
</tbody></table>
<p><strong>举例说明：</strong></p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line">[<span class="type">root</span>@<span class="type">MissHou</span>  <span class="type">shell01</span>]<span class="comment"># cat 3.txt 	自己创建该文件用于测试</span></span><br><span class="line">ROOT:x:<span class="number">0</span>:<span class="number">0</span>:root:/root:/bin/bash</span><br><span class="line">bin:x:<span class="number">1</span>:<span class="number">1</span>:bin:/bin:/sbin/nologin</span><br><span class="line">daemon:x:<span class="number">2</span>:<span class="number">2</span>:daemon:/sbin:/sbin/nologin</span><br><span class="line">adm:x:<span class="number">3</span>:<span class="number">4</span>:adm:/var/adm:/sbin/nologin</span><br><span class="line"><span class="built_in">lp</span>:x:<span class="number">4</span>:<span class="number">7</span>:<span class="built_in">lp</span>:/var/spool/lpd:/sbin/nologin</span><br><span class="line">sync:x:<span class="number">5</span>:<span class="number">0</span>:sync:/sbin:/bin/sync</span><br><span class="line">shutdown:x:<span class="number">6</span>:<span class="number">0</span>:shutdown:/sbin:/sbin/shutdown</span><br><span class="line">halt:x:<span class="number">7</span>:<span class="number">0</span>:halt:/sbin:/sbin/halt</span><br><span class="line">mail:x:<span class="number">8</span>:<span class="number">12</span>:mail:/var/spool/mail:/sbin/nologin</span><br><span class="line">uucp:x:<span class="number">10</span>:<span class="number">14</span>:uucp:/var/spool/uucp:/sbin/nologin</span><br><span class="line">boss02:x:<span class="number">516</span>:<span class="number">511</span>::/home/boss02:/bin/bash</span><br><span class="line">vip:x:<span class="number">517</span>:<span class="number">517</span>::/home/vip:/bin/bash</span><br><span class="line">stu1:x:<span class="number">518</span>:<span class="number">518</span>::/home/stu1:/bin/bash</span><br><span class="line">mailnull:x:<span class="number">47</span>:<span class="number">47</span>::/var/spool/mqueue:/sbin/nologin</span><br><span class="line">smmsp:x:<span class="number">51</span>:<span class="number">51</span>::/var/spool/mqueue:/sbin/nologin</span><br><span class="line">aaaaaaaaaaaaaaaaaaaa</span><br><span class="line">bbbbbb111111122222222222233333333cccccccc</span><br><span class="line">hello world <span class="number">888</span></span><br><span class="line"><span class="number">666</span></span><br><span class="line"><span class="number">777</span></span><br><span class="line"><span class="number">999</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># tr -d &#x27;[:/]&#x27; &lt; 3.txt 				删除文件中的:和/</span></span><br><span class="line"><span class="comment"># cat 3.txt |tr -d &#x27;[:/]&#x27;			删除文件中的:和/</span></span><br><span class="line"><span class="comment"># tr &#x27;[0-9]&#x27; &#x27;@&#x27; &lt; 3.txt 			将文件中的数字替换为@符号</span></span><br><span class="line"><span class="comment"># tr &#x27;[a-z]&#x27; &#x27;[A-Z]&#x27; &lt; 3.txt 		将文件中的小写字母替换成大写字母</span></span><br><span class="line"><span class="comment"># tr -s &#x27;[a-z]&#x27; &lt; 3.txt 			匹配小写字母并将重复的压缩为一个</span></span><br><span class="line"><span class="comment"># tr -s &#x27;[a-z0-9]&#x27; &lt; 3.txt 		匹配小写字母和数字并将重复的压缩为一个</span></span><br><span class="line"><span class="comment"># tr -d &#x27;[:digit:]&#x27; &lt; 3.txt 		删除文件中的数字</span></span><br><span class="line"><span class="comment"># tr -d &#x27;[:blank:]&#x27; &lt; 3.txt 		删除水平空白</span></span><br><span class="line"><span class="comment"># tr -d &#x27;[:space:]&#x27; &lt; 3.txt 		删除所有水平和垂直空白</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h4 id="小试牛刀"><a href="#小试牛刀" class="headerlink" title="小试牛刀"></a>小试牛刀</h4><ol>
<li>使用小工具分别截取当前主机IP；截取NETMASK；截取广播地址；截取MAC地址</li>
</ol>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># ifconfig eth0|grep &#x27;Bcast&#x27;|tr -d &#x27;[a-zA-Z ]&#x27;|cut -d: -f2,3,4</span></span><br><span class="line"><span class="number">10.1</span>.<span class="number">1.1</span>:<span class="number">10.1</span>.<span class="number">1.255</span>:<span class="number">255.255</span>.<span class="number">255.0</span></span><br><span class="line"><span class="comment"># ifconfig eth0|grep &#x27;Bcast&#x27;|tr -d &#x27;[a-zA-Z ]&#x27;|cut -d: -f2,3,4|tr &#x27;:&#x27; &#x27;\n&#x27;</span></span><br><span class="line"><span class="number">10.1</span>.<span class="number">1.1</span></span><br><span class="line"><span class="number">10.1</span>.<span class="number">1.255</span></span><br><span class="line"><span class="number">255.255</span>.<span class="number">255.0</span></span><br><span class="line"><span class="comment"># ifconfig eth0|grep &#x27;HWaddr&#x27;|cut -d: -f2-|cut -d&#x27; &#x27; -f4</span></span><br><span class="line"><span class="number">00</span>:<span class="number">0</span>C:<span class="number">29</span>:<span class="number">25</span>:AE:<span class="number">54</span></span><br><span class="line"><span class="comment"># ifconfig eth0|grep &#x27;HW&#x27;|tr -s &#x27; &#x27;|cut -d&#x27; &#x27; -f5</span></span><br><span class="line"><span class="number">00</span>:<span class="number">0</span>C:<span class="number">29</span>:B4:<span class="number">9</span>E:<span class="number">4</span>E</span><br><span class="line"></span><br><span class="line"><span class="comment"># ifconfig eth1|grep Bcast|cut -d: -f2|cut -d&#x27; &#x27; -f1</span></span><br><span class="line"><span class="comment"># ifconfig eth1|grep Bcast|cut -d: -f2|tr -d &#x27;[ a-zA-Z]&#x27;</span></span><br><span class="line"><span class="comment"># ifconfig eth1|grep Bcast|tr -d &#x27;[:a-zA-Z]&#x27;|tr &#x27; &#x27; &#x27;@&#x27;|tr -s &#x27;@&#x27;|tr &#x27;@&#x27; &#x27;\n&#x27;|grep -v ^$</span></span><br><span class="line"><span class="comment"># ifconfig eth0|grep &#x27;Bcast&#x27;|tr -d [:alpha:]|tr &#x27;[ :]&#x27; &#x27;\n&#x27;|grep -v ^$</span></span><br><span class="line"><span class="comment"># ifconfig eth1|grep HWaddr|cut -d &#x27; &#x27; -f11</span></span><br><span class="line"><span class="comment"># ifconfig eth0|grep HWaddr|tr -s &#x27; &#x27;|cut -d&#x27; &#x27; -f5</span></span><br><span class="line"><span class="comment"># ifconfig eth1|grep HWaddr|tr -s &#x27; &#x27;|cut -d&#x27; &#x27; -f5</span></span><br><span class="line"><span class="comment"># ifconfig eth0|grep &#x27;Bcast&#x27;|tr -d &#x27;a-zA-Z:&#x27;|tr &#x27; &#x27; &#x27;\n&#x27;|grep -v &#x27;^$&#x27;</span></span><br></pre></td></tr></table></figure>
<ol start="2">
<li>将系统中所有普通用户的用户名、密码和默认shell保存到一个文件中，要求用户名密码和默认shell之间用tab键分割</li>
</ol>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># grep &#x27;bash$&#x27; passwd |grep -v &#x27;root&#x27;|cut -d: -f1,2,7|tr &#x27;:&#x27; &#x27;\t&#x27; |tee abc.txt</span></span><br></pre></td></tr></table></figure>
<h1 id="二、bash的特性"><a href="#二、bash的特性" class="headerlink" title="二、bash的特性"></a>二、bash的特性</h1><p>##1、命令和文件自动补全</p>
<p>Tab只能补全==命令和文件== （RHEL6/Centos6）</p>
<p>##2、常见的快捷键</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">^c   			终止前台运行的程序</span><br><span class="line">^z	  			将前台运行的程序挂起到后台</span><br><span class="line">^d   			退出 等价<span class="keyword">exit</span></span><br><span class="line">^l   			清屏 </span><br><span class="line">^a |home  	光标移到命令行的最前端</span><br><span class="line">^e |<span class="keyword">end</span>  	光标移到命令行的后端</span><br><span class="line">^u   			删除光标前所有字符</span><br><span class="line">^k   			删除光标后所有字符</span><br><span class="line">^r	 			搜索历史命令</span><br></pre></td></tr></table></figure>
<p>##3 、==常用的通配符（重点）==</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">*:	匹配<span class="number">0</span>或多个任意字符</span><br><span class="line">?:	匹配任意单个字符</span><br><span class="line">[<span class="type">list</span>]:	匹配[<span class="type">list</span>]中的任意单个字符,或者一组单个字符   [<span class="type">a</span>-<span class="type">z</span>]</span><br><span class="line">[!<span class="type">list</span>]: 匹配除list中的任意单个字符</span><br><span class="line">&#123;string1,string2,...&#125;：匹配string1,string2或更多字符串</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># rm -f file*</span></span><br><span class="line"><span class="comment"># cp *.conf  /dir1</span></span><br><span class="line"><span class="comment"># touch file&#123;1..5&#125;</span></span><br></pre></td></tr></table></figure>
<p>##4、==bash中的引号（重点）==</p>
<ul>
<li>双引号””   :会把引号的内容当成整体来看待，允许通过$符号引用其他变量值</li>
<li>单引号’’     :会把引号的内容当成整体来看待，禁止引用其他变量值，shell中特殊符号都被视为普通字符</li>
<li>反撇号``  :反撇号和$()一样，引号或括号里的命令会优先执行，如果存在嵌套，反撇号不能用</li>
</ul>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">[<span class="type">root</span>@<span class="type">MissHou</span>  <span class="type">dir1</span>]<span class="comment"># echo &quot;$(hostname)&quot;</span></span><br><span class="line">server</span><br><span class="line">[<span class="type">root</span>@<span class="type">MissHou</span>  <span class="type">dir1</span>]<span class="comment"># echo &#x27;$(hostname)&#x27;</span></span><br><span class="line"><span class="variable">$</span>(hostname)</span><br><span class="line">[<span class="type">root</span>@<span class="type">MissHou</span>  <span class="type">dir1</span>]<span class="comment"># echo &quot;hello world&quot;</span></span><br><span class="line">hello world</span><br><span class="line">[<span class="type">root</span>@<span class="type">MissHou</span>  <span class="type">dir1</span>]<span class="comment"># echo &#x27;hello world&#x27;</span></span><br><span class="line">hello world</span><br><span class="line"></span><br><span class="line">[<span class="type">root</span>@<span class="type">MissHou</span>  <span class="type">dir1</span>]<span class="comment"># echo $(date +%F)</span></span><br><span class="line"><span class="number">2018</span><span class="literal">-11</span><span class="literal">-22</span></span><br><span class="line">[<span class="type">root</span>@<span class="type">MissHou</span>  <span class="type">dir1</span>]<span class="comment"># echo `echo $(date +%F)`</span></span><br><span class="line"><span class="number">2018</span><span class="literal">-11</span><span class="literal">-22</span></span><br><span class="line">[<span class="type">root</span>@<span class="type">MissHou</span>  <span class="type">dir1</span>]<span class="comment"># echo `date +%F`</span></span><br><span class="line"><span class="number">2018</span><span class="literal">-11</span><span class="literal">-22</span></span><br><span class="line">[<span class="type">root</span>@<span class="type">MissHou</span>  <span class="type">dir1</span>]<span class="comment"># echo `echo `date +%F``</span></span><br><span class="line">date +%F</span><br><span class="line">[<span class="type">root</span>@<span class="type">MissHou</span>  <span class="type">dir1</span>]<span class="comment"># echo $(echo `date +%F`)</span></span><br><span class="line"><span class="number">2018</span><span class="literal">-11</span><span class="literal">-22</span></span><br></pre></td></tr></table></figure>


    </div>

    
    
    

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/linux/" rel="tag"># linux</a>
              <a href="/tags/shell/" rel="tag"># shell</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2021/01/01/%E5%B0%8F%E7%94%9F%E5%8D%9A%E5%AE%A2/%E6%95%B0%E6%8D%AE%E5%BA%93/mycat/" rel="prev" title="mycat使用">
      <i class="fa fa-chevron-left"></i> mycat使用
    </a></div>
      <div class="post-nav-item">
    <a href="/2021/01/02/%E5%B0%8F%E7%94%9F%E5%8D%9A%E5%AE%A2/linux/02_SHELL%E7%BC%96%E7%A8%8B%E4%B9%8B%E5%8F%98%E9%87%8F%E5%AE%9A%E4%B9%89/" rel="next" title="02_SHELL编程之变量定义">
      02_SHELL编程之变量定义 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E6%9C%AC%E8%8A%82%E8%AF%BE%E7%A8%8B%E7%9B%AE%E6%A0%87"><span class="nav-number">1.</span> <span class="nav-text">本节课程目标</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-grep%E5%B7%A5%E5%85%B7"><span class="nav-number">1.1.</span> <span class="nav-text">1. grep工具</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%AF%AD%E6%B3%95%E5%92%8C%E9%80%89%E9%A1%B9"><span class="nav-number">1.1.1.</span> <span class="nav-text">语法和选项</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-cut%E5%B7%A5%E5%85%B7"><span class="nav-number">1.2.</span> <span class="nav-text">2. cut工具</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%AF%AD%E6%B3%95%E5%92%8C%E9%80%89%E9%A1%B9-1"><span class="nav-number">1.2.1.</span> <span class="nav-text">语法和选项</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-sed"><span class="nav-number">1.3.</span> <span class="nav-text">3. sed</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%AF%AD%E6%B3%95%E5%92%8C%E9%80%89%E9%A1%B9-2"><span class="nav-number">1.3.1.</span> <span class="nav-text">语法和选项</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-awk"><span class="nav-number">1.4.</span> <span class="nav-text">4. awk</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%AF%AD%E6%B3%95%E5%92%8C%E9%80%89%E9%A1%B9-3"><span class="nav-number">1.4.1.</span> <span class="nav-text">语法和选项</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#5-sort%E5%B7%A5%E5%85%B7"><span class="nav-number">1.5.</span> <span class="nav-text">5. sort工具</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%AF%AD%E6%B3%95%E5%92%8C%E9%80%89%E9%A1%B9-4"><span class="nav-number">1.5.1.</span> <span class="nav-text">语法和选项</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#6-uniq%E5%B7%A5%E5%85%B7"><span class="nav-number">1.6.</span> <span class="nav-text">6. uniq工具</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#7-tee%E5%B7%A5%E5%85%B7"><span class="nav-number">1.7.</span> <span class="nav-text">7. tee工具</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#8-diff%E5%B7%A5%E5%85%B7"><span class="nav-number">1.8.</span> <span class="nav-text">8. diff工具</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%AF%AD%E6%B3%95%E5%92%8C%E9%80%89%E9%A1%B9-5"><span class="nav-number">1.8.1.</span> <span class="nav-text">语法和选项</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#9-paste%E5%B7%A5%E5%85%B7"><span class="nav-number">1.9.</span> <span class="nav-text">9. paste工具</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#10-tr%E5%B7%A5%E5%85%B7"><span class="nav-number">1.10.</span> <span class="nav-text">10. tr工具</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%AF%AD%E6%B3%95%E5%92%8C%E9%80%89%E9%A1%B9-6"><span class="nav-number">1.10.1.</span> <span class="nav-text">语法和选项</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%B0%8F%E8%AF%95%E7%89%9B%E5%88%80"><span class="nav-number">1.10.1.1.</span> <span class="nav-text">小试牛刀</span></a></li></ol></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%BA%8C%E3%80%81bash%E7%9A%84%E7%89%B9%E6%80%A7"><span class="nav-number">2.</span> <span class="nav-text">二、bash的特性</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="王增明"
      src="/images/avatar.jpeg">
  <p class="site-author-name" itemprop="name">王增明</p>
  <div class="site-description" itemprop="description">喜欢折腾的极客聚集地.</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">23</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">6</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">18</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <span class="exturl" data-url="aHR0cHM6Ly9naXRodWIuY29tL3dhbnplbmdtaW5n" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;wanzengming"><i class="fab fa-github fa-fw"></i>GitHub</span>
      </span>
      <span class="links-of-author-item">
        <a href="/wangzengming@aliyun.com" title="E-Mail → wangzengming@aliyun.com"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
  </div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">王增明</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-area"></i>
    </span>
      <span class="post-meta-item-text">站点总字数：</span>
    <span title="站点总字数">342k</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
      <span class="post-meta-item-text">站点阅读时长 &asymp;</span>
    <span title="站点阅读时长">5:11</span>
</div>

        
<div class="busuanzi-count">
  <script data-pjax async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/pjax/pjax.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>

<script src="/js/bookmark.js"></script>

  <script>
var pjax = new Pjax({
  selectors: [
    'head title',
    '#page-configurations',
    '.content-wrap',
    '.post-toc-wrap',
    '.languages',
    '#pjax'
  ],
  switches: {
    '.post-toc-wrap': Pjax.switches.innerHTML
  },
  analytics: false,
  cacheBust: false,
  scrollTo : !CONFIG.bookmark.enable
});

window.addEventListener('pjax:success', () => {
  document.querySelectorAll('script[data-pjax], script#page-configurations, #pjax script').forEach(element => {
    var code = element.text || element.textContent || element.innerHTML || '';
    var parent = element.parentNode;
    parent.removeChild(element);
    var script = document.createElement('script');
    if (element.id) {
      script.id = element.id;
    }
    if (element.className) {
      script.className = element.className;
    }
    if (element.type) {
      script.type = element.type;
    }
    if (element.src) {
      script.src = element.src;
      // Force synchronous loading of peripheral JS.
      script.async = false;
    }
    if (element.dataset.pjax !== undefined) {
      script.dataset.pjax = '';
    }
    if (code !== '') {
      script.appendChild(document.createTextNode(code));
    }
    parent.appendChild(script);
  });
  NexT.boot.refresh();
  // Define Motion Sequence & Bootstrap Motion.
  if (CONFIG.motion.enable) {
    NexT.motion.integrator
      .init()
      .add(NexT.motion.middleWares.subMenu)
      .add(NexT.motion.middleWares.postList)
      .bootstrap();
  }
  NexT.utils.updateSidebarPosition();
});
</script>


  <script defer src="/lib/three/three.min.js"></script>
    <script defer src="/lib/three/three-waves.min.js"></script>
    <script defer src="/lib/three/canvas_lines.min.js"></script>
    <script defer src="/lib/three/canvas_sphere.min.js"></script>


  
  <script data-pjax>
    (function(){
      var canonicalURL, curProtocol;
      //Get the <link> tag
      var x=document.getElementsByTagName("link");
		//Find the last canonical URL
		if(x.length > 0){
			for (i=0;i<x.length;i++){
				if(x[i].rel.toLowerCase() == 'canonical' && x[i].href){
					canonicalURL=x[i].href;
				}
			}
		}
    //Get protocol
	    if (!canonicalURL){
	    	curProtocol = window.location.protocol.split(':')[0];
	    }
	    else{
	    	curProtocol = canonicalURL.split(':')[0];
	    }
      //Get current URL if the canonical URL does not exist
	    if (!canonicalURL) canonicalURL = window.location.href;
	    //Assign script content. Replace current URL with the canonical URL
      !function(){var e=/([http|https]:\/\/[a-zA-Z0-9\_\.]+\.baidu\.com)/gi,r=canonicalURL,t=document.referrer;if(!e.test(r)){var n=(String(curProtocol).toLowerCase() === 'https')?"https://sp0.baidu.com/9_Q4simg2RQJ8t7jm9iCKT-xh_/s.gif":"//api.share.baidu.com/s.gif";t?(n+="?r="+encodeURIComponent(document.referrer),r&&(n+="&l="+r)):r&&(n+="?l="+r);var i=new Image;i.src=n}}(window);})();
  </script>




  
<script src="/js/local-search.js"></script>











<script data-pjax>
if (document.querySelectorAll('pre.mermaid').length) {
  NexT.utils.getScript('//cdn.jsdelivr.net/npm/mermaid@8/dist/mermaid.min.js', () => {
    mermaid.initialize({
      theme    : 'forest',
      logLevel : 3,
      flowchart: { curve     : 'linear' },
      gantt    : { axisFormat: '%m/%d/%Y' },
      sequence : { actorMargin: 50 }
    });
  }, window.mermaid);
}
</script>


    <div id="pjax">
  

  

    </div>
<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","model":{"jsonPath":"/live2dw/assets/hijiki.model.json"},"display":{"position":"right","width":150,"height":300},"mobile":{"show":false},"react":{"opacityDefault":1,"opacityOnHover":1},"log":false,"tagMode":false});</script></body>
</html>
<script type="text/javascript" src="//cdn.bootcss.com/canvas-nest.js/1.0.0/canvas-nest.min.js"></script>
